// pages/match/match.js
//const match =require('../../utils/match')
var match = require('../../utils/match');
Page({
tijiao(){
    var that = this;
    var matchid = this.data.matchid;
    var index = this.data.category;
    var userid = this.data.userid;
    var username = this.data.username;
    var time = new Date();
    var image = null;
    wx.chooseMedia({
      count: 1,
      success(res) {
       
          console.log(res.tempFiles)
          var po = res.tempFiles[0].tempFilePath.lastIndexOf(".")
          var ext = res.tempFiles[0].tempFilePath.slice(po)
          wx.cloud.uploadFile({
              cloudPath: matchid+index+userid + ext,
              filePath: res.tempFiles[0].tempFilePath,
              success(res) {
                  console.log(res.fileID);
                  image=res.fileID;
                  that.setData({
                    currentImage:image
                  });
                var database = wx.cloud.database();
                database.collection("tb_item").add({
                  data:{
                    
                      matchid:matchid,
                      index:index,
                      userid:userid,
                      username:username,
                      time:time,
                      image:image
                    
                  }
                }).then((s)=>{
                  if(s)wx.showToast({
                    title: '提交图片成功',
                  })
                })
          
      }
  })
     }
     })
},
  click:function(e){
    this.setData({
      category:e.currentTarget.id,
    });
    
    var that = this;
    console.log("场景下标: "+this.data.category);
    var database = wx.cloud.database();
    database.collection("tb_item").where({
      matchid:that.data.matchid,
      index:that.data.category+""
    }).get().then((res)=>{
      that.setData({
        xuanshou:res.data
      });
      for(let i=0;i<that.data.xuanshou.length;i++){
        var time = that.data.xuanshou[i].time;
        var d = time.toLocaleDateString().substring(0,10);
        var t = time.toTimeString().substring(0,8);
        var time = d+" "+t;
        that.data.xuanshou[i].time = time;
      }
      that.setData({ 
        xuanshou:that.data.xuanshou
      });
      
    })
},  
dianji(){
  var k = this.data.category;
  var img=this.data.imageList[k]
  this.setData({dialogShow_match:true});
  currentImage:img
},
    /**
     * 页面的初始数据
     */
    data: {
         xuanshou:[],
         currentImage:null,
         userid:null,
         matchid:null,
         matchname:null,
         picnames:[],
         imageList:[],
         xuanshou:[],
         username:null,
         matches:match.list,
         category:0,
         id:null,
         dialogShow_match:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      var that=this;
        wx.getStorage({
         key: "userid",
          success(res){
             that.setData({
               userid:res.data
             });
             //到数据库里面找到当前用户参赛的比赛
             var database = wx.cloud.database();
             database.collection("tb_user").where({
               _id:that.data.userid
             }).get().then((r)=>{
               that.setData({
                 username:r.data[0].name
               });
             })
             database.collection("tb_baoming").where({
               userid:that.data.userid
             }).get().then((msg)=>{
               that.setData({
                 matchid:msg.data[0].matchid,
                 matchname:msg.data[0].matchname
               });
               wx.setNavigationBarTitle({
                 title: msg.data[0].matchname,
               });
               database.collection('tb_item').where({
                 matchid:that.data.matchid,
                 index:0
               }).get().then((g)=>{
                 that.setData({
                   xuanshou:g.data
                 });
                 //把选手的时间字段变成可以理解的内容
                 for(let i=0;i<that.data.xuanshou.length;i++){
                   var time = that.data.xuanshou[i].time;
                   var d = time.toLocaleDateString().substring(0,10);
                   var t = time.toTimeString().substring(0,8);
                   var time = d+" "+t;
                   that.data.xuanshou[i].time = time;
                 }
                 that.setData({ 
                   xuanshou:that.data.xuanshou
                 });
               });
               database.collection("tb_match").where({
                 _id:that.data.matchid
               }).get().then((m)=>{
                
                 var imageList = m.data[0].imageList;
                
                 var picnames = [];
                 for(let i=0; i<imageList.length;i++){
                   var data =imageList[i];
                   var loc=data.lastIndexOf("/");
                   data = data.slice(loc+1);
                   loc = data.lastIndexOf(".");
                   data=data.slice(0,loc);
                   picnames.push(data);
                 } 
                 that.setData({
                   imageList:imageList,
                   picnames:picnames

                 });
               });
              })
          }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})